<%@page import="com.tubeonfire.model.admin.PlaylistModel"%>
<%@page import="com.tubeonfire.entity.Tube"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="com.tubeonfire.entity.Playlist"%>
<%@page import="java.util.TreeMap"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	ImagesService imagesService = ImagesServiceFactory.getImagesService(); 		
	PlaylistModel plModel = new PlaylistModel();
	plModel.prepareAll(true);
	String action = (String)session.getAttribute("action");
	Tube obj = (Tube)session.getAttribute("obj");	
	TreeMap check = (TreeMap)session.getAttribute("check");
	session.removeAttribute("check");
	if(check==null){
		check = new TreeMap();
	}
%>
<jsp:include page="/admin/elements/header.jsp">
	<jsp:param value="Tube Form" name="title"/>
</jsp:include>
<script type="text/javascript" src="/admin/js/mine/form_tube.js"></script>
<script type="text/javascript">
	jQuery(document).ready(function() { 		
		jQuery("#btnSubmit").click(function(){			
			onSubmitForm("<%=action%>");
			return false;
		}); 		
	});    	
	function onSubmitForm(action){         	
    	jQuery.get("/admin/upload/get-url?returnUrl=" + "/admin/tube/" + action, function(data){        		
    		jQuery("#tubeForm").attr("action", data);      		
    		jQuery("#tubeForm").submit();    		
    	});        	
    }
</script>

	<body class="loggedin">
		<jsp:include page="/admin/elements/top.jsp"/>
	    <!-- START OF MAIN CONTENT -->
	    <div class="mainwrapper">
	     	<div class="mainwrapperinner">         	        
				<jsp:include page="/admin/elements/left.jsp">
					<jsp:param value="tube" name="current_page"/>
				</jsp:include>							              	                   
	        	<div class="maincontent noright">
	        		<div class="maincontentinner">	            	
		                <ul class="maintabmenu">
		                	<li><a href="/admin/youtube/search">Youtube Search</a></li>
		                	<li><a href="/admin/tube/list">Tube Manager</a></li>
		                	<li><a href="/admin/tube/slideshow">Slide Show Manager</a></li>
		                	<li class="current"><a href="#">
		                		<%
	                				if(action!=null&&action.equals("add")){
	                					%>
	                					New Tube
	                					<%
	                				}else if(action!=null&&action.equals("edit")){
	                					%>
	                					Edit Tube
	                					<%
	                				}
                				%>    
                			</a></li>
		                </ul><!--maintabmenu-->	                
	                	<div class="content">	
	                	<jsp:include page="/admin/elements/message.jsp"/>
	                	<div id="messageDiv" class="notification hidden">						   
						    <p></p>
						</div>	                    
	                    	<div class="contenttitle">
	                    		<h2 class="image"><span>Fill Tube Information</span></h2>
	                    	</div>
	                    	<br />
		                    <form class="stdform" enctype="multipart/form-data"  action="" method="post" id="tubeForm">
		                    <%
		                    	if(action.equals("edit")){
		                    		%>
		                    		<input type="hidden" name="id" value="<%=obj.getId()%>">		                    			                    	
		                    		<%
		                    	}
		                    %>    
		                    	<p>
									<label>Choose playlist</label>		
									<%
                                    	if(plModel!=null){
                                    %>							
                                    <span class="field">                                    
                                    	<select name="playlist">
	                                    	<%
					                    		for(Playlist pl : plModel.getListResult()){			                    						                    		
					                    	%>
					                        	<option <%=(obj.getPlaylistId().equals(pl.getId())?"selected":"") %> value="<%=pl.getId()%>"><%=pl.getTitle() %></option>
					                        <%
					                			}
					                        %>                                            	                                           
                                        </select>                                              	
                                        <% if (check.get("playlist") != null) { %>
											<label for="playlist" class="error"><%=check.get("playlist") %></label>
										<% } %>          
                                    </span>   
                                    <%
                                    	}
                                    %>                                     	                                                                                         
                                </p>                 	
		                        <p>
		                        	<label>Title</label>
		                            <span class="field">
		                            	<input type="text" name="title" id="title" class="smallinput" value="<%=obj.getTitle()%>"/>
		                            	&nbsp;<a href="/video/<%=obj.getId() %>/<%=obj.getAlias() %>.html">(client view)</a>
		                            	<% if (check.get("title") != null) { %>
											<label for="title" class="error"><%=check.get("title") %></label>
										<% } %>
		                            </span>		                            		                           
		                        </p>                                                                      
		                        <p>
		                        	<label>Description</label>
		                            <span class="field">
		                            	<textarea cols="80" rows="5" class="longinput" name="description" id="description"><%=obj.getDescription()%></textarea>		                            
			                            <% if (check.get("description") != null) { %>
											<label for="description" class="error"><%=check.get("description") %></label>
										<% } %> 
									</span>
									<small class="desc">Small description of this tube.</small>
		                        </p>    		                                         
		                        <p>
		                        	<label>Cover Image</label>
		                            <span class="field">
		                            	<input type="file" name="img" class="smallinput">
		                            	<% if (check.get("img") != null) { %>
											<label for="img" class="error"><%=check.get("img") %></label>
										<% } %>
										<br/><br/>
										<%
											if(!obj.getImageUrl().contains("http://")){
												 try{
						                        	   	BlobKey blobKey = new BlobKey(obj.getImageUrl());
					                            		String imgSrc = imagesService.getServingUrl(blobKey);
						                           %>
						                            		<img src="<%=imgSrc %>=s250" alt="" style="max-width: 250px; max-height: 150px"/>
						                           <%	
						                            }catch(Exception e){
						                            			
						                            }
											}else{
												%>
												<img src="<%=obj.getImageUrl() %>" alt="" style="max-width: 250px; max-height: 150px"/>
												<%
											}				                          
				                          %>	   
		                            </span> 
		                        </p>   
		                        <br clear="all" /><br clear="all" />
		                        <p>
		                        	<label>Tags</label>
		                            <span class="field">
		                            	<%
		                            		String tags = "";		                            	
		                            		for(int i=0; i<obj.getTags().size();i++){
		                            			if(i==obj.getTags().size()-1){
		                            				tags+= obj.getTags().get(i);
		                            			}else{
		                            				tags+= obj.getTags().get(i)+", ";	
		                            			}		                            			
		                            		}
		                            	%>
		                            	<input type="text" name="tags" id="tags" class="longinput" value="<%=tags%>"/>
		                            	<% if (check.get("tags") != null) { %>
											<label for="tags" class="error"><%=check.get("tags") %></label>
										<% } %>
									</span>
									<small class="desc">Separated by comma. Example : "tag, my tag, your tag".</small>
		                        </p>   		                                                                                                                                                             
                        		<br clear="all" />                  
		                        <p class="stdformbutton">
		                        	<button class="submit radius2" id="btnSubmit">Submit</button>
		                            <input type="reset" class="reset radius2" value="Reset" />
		                            <span style="padding-left: 30px">		                            	
		                         		<a href="<%=obj.getId() %>" id="btnBanner" class="btn btn_bulb" title="Make tube display in banner at homepage"><span>Set Slide Show Video</span></a>&nbsp;&nbsp;
		                         		<a href="<%=obj.getId() %>" id="btnDelete" class="btn btn_trash" title="Delete this tube"><span>Delete</span></a>
		                            </span>		                            		                              		                          
		                        </p>            		                                                          
                    		</form>             
	                    <br /><br />	                    
	                </div><!--content-->	                
	            </div><!--maincontentinner-->
				<jsp:include page="/admin/elements/bottom.jsp"></jsp:include>                    	           	
	        </div><!--maincontent-->	                        
	     	</div><!--mainwrapperinner-->
	    </div><!--mainwrapper-->
	     <div id="opacity"></div>
		<img id="image_loadder" src="/admin/images/ajax-loader.gif">   
		<!-- END OF MAIN CONTENT -->    
	</body>
</html>
